import * as vscode from 'vscode';
import { AgentViewProvider } from './AgentViewProvider';
import { MessageHandler } from '../utils/MessageHandler';
import { LLMMessageHandler } from './message-handlers/LLMMessageHandler';
import { StoreMessageHandler } from './message-handlers/StoreMessageHandler';
import { Logger } from '../utils/Logger';

export class AgentViewController {
	
	handlers: MessageHandler[] = [];

	handleReceivedWebViewMessage(message: any) {
		Logger.log('AI Code Agent: received webView message');
		Logger.log(JSON.stringify(message));
		try {
			this.handlers.forEach((handler) => {
				handler.handleMessage(message);
			});
		} catch (error) {
			Logger.log('AI Code Agent: handle message error');
			Logger.log(JSON.stringify(error));
		}
	}

	sendMessageToWebView(message: any) {
		Logger.log('AI Code Agent: send message to webView');
		Logger.log(JSON.stringify(message));
		this.viewProvider.view?.webview.postMessage(message);
	}

	constructor(
		readonly context: vscode.ExtensionContext,
		private readonly outputChannel: vscode.OutputChannel,
		readonly viewProvider: AgentViewProvider
	) {
		this.handlers = [
			new LLMMessageHandler(this),
			new StoreMessageHandler(this)
		];
	}
}